{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "########### 1.输出+类型转换 ###########"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入第一个数：1\n",
      "输入第二个数：2\n",
      "两数之和：3\n"
     ]
    }
   ],
   "source": [
    "user_num1=input(\"输入第一个数：\")\n",
    "user_num2=input(\"输入第二个数：\")\n",
    "\n",
    "print(\"两数之和：%d\"%(int(user_num1)+int(user_num2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "########### 2.字符串拼接+拼接输出方式 ###########"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入昵称：tom\n",
      "输入密码：123456\n",
      "ftp://tom:123456@192.168.1.121\n",
      "ftp://tom:123456@192.168.1.121\n"
     ]
    }
   ],
   "source": [
    "user_name=input(\"输入昵称：\")\n",
    "user_pass=input(\"输入密码：\")\n",
    "user_url=\"192.168.1.121\"\n",
    "\n",
    "#拼接输出方式一：\n",
    "print(\"ftp://\"+user_name+\":\"+user_pass+\"@\"+user_url)\n",
    "\n",
    "#拼接输出方式二：\n",
    "print(\"ftp://%s:%s@%s\"%(user_name,user_pass,user_url))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "########### 3.字符串遍历、下标、切片 ###########"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_str=\"七大姑曰：工作了吗？八大姨问：买房了吗？异性说：结婚了吗？\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "七 大 姑 曰 ： 工 作 了 吗 ？ 八 大 姨 问 ： 买 房 了 吗 ？ 异 性 说 ： 结 婚 了 吗 ？ "
     ]
    }
   ],
   "source": [
    "#遍历\n",
    "for item in user_str:\n",
    "    print(item,end=\" \") #不换行，以“ ”方式拼接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "29\n"
     ]
    }
   ],
   "source": [
    "#长度：len(user_str)\n",
    "print(len(user_str))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "七\n"
     ]
    }
   ],
   "source": [
    "# #第一个元素：user_str[0]\n",
    "print(user_str[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "？\n",
      "？\n"
     ]
    }
   ],
   "source": [
    "#最后一个元素：user_str[-1]\n",
    "print(user_str[-1])\n",
    "print(user_str[len(user_str)-1])#其他编程语言写法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "吗\n"
     ]
    }
   ],
   "source": [
    "#倒数第二个元素：user_str[-2]\n",
    "print(user_str[-2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# python切片语法：[start_index:end_index:step] （end_index取不到）\n",
    "\n",
    "# eg:str[1:4] 取str[1]、str[2]、str[3]\n",
    "# eg:str[2:] 取下标为2开始到最后的元素\n",
    "# eg:str[2:-1] 取下标为2～到倒数第二个元素（end_index取不到）\n",
    "# eg:str[1:6:2] 隔着取～str[1]、str[3]、str[5]（案例会详细说）\n",
    "# eg:str[::-1] 逆向输出（案例会详细说，）\n",
    "\n",
    "it_str=\"我爱编程，编程爱它，它是程序，程序是谁？\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "编程爱它\n",
      "编程爱它\n",
      "编程爱它\n"
     ]
    }
   ],
   "source": [
    "# eg:取“编程爱它” it_str[5:9]\n",
    "print(it_str[5:9])\n",
    "print(it_str[5:-11]) #end_index用-xx也一样\n",
    "print(it_str[-15:-11])#start_index用-xx也可以"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "编程爱它，它是程序，程序是谁？\n"
     ]
    }
   ],
   "source": [
    "# eg:取“编程爱它，它是程序，程序是谁？” it_str[5:]\n",
    "print(it_str[5:])#不写默认取到最后一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我编，程它它程，序谁\n"
     ]
    }
   ],
   "source": [
    "#eg:一个隔一个跳着取（\"我编，程它它程，序谁\"） it_str[0::2]\n",
    "print(it_str[0::2])#step=△index（eg:0,1,2,3。这里的step=> 2-0 => 间隔1）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "？谁是序程，序程是它，它爱程编，程编爱我\n",
      "？谁是序程，序程是它，它爱程编，程编爱我\n"
     ]
    }
   ],
   "source": [
    "#eg:倒序输出 it_str[::-1]\n",
    "# end_index不写默认是取到最后一个，是正取（从左往右）还是逆取（从右往左），就看step是正是负\n",
    "print(it_str[::-1])\n",
    "print(it_str[-1::-1])#等价于上一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
